<?= $this->Html->script('ajax-forms', array('inline'=>false)) ?>

<?= $this->Form->create('KalkulatorOsiguranje', array(
    'url'   => '/admin_cene/save_osiguranje',
    'class' => 'ajax-form',
    'id'    => 'osiguranje_cene'
)) ?>
    
    <? foreach ($data as $vrsta_vozila => $d): ?>
    <table class="entity-table">
        <tr>
            <td colspan="4"><?= $lblVrsteVozila[$vrsta_vozila] ?></td>
        </tr>
        <tr>
            <th class="text-right">min (<?= $units[$vrsta_vozila] ?>)</th>
            <th class="text-right">max (<?= $units[$vrsta_vozila] ?>)</th>
            <th class="text-right">cena</th>
            <th></th>
        </tr>
        <? 
        foreach ($d as $i=>$row):
            $key = 'KalkulatorOsiguranje';
            $row = $row[$key]; 
        ?>
        <tr index="<?= $i ?>">
            <?= $this->Form->hidden("{$vrsta_vozila}.{$i}.{$key}.id", array('value' => @$row['id'])) ?>
            <?= $this->Form->hidden("{$vrsta_vozila}.{$i}.{$key}.vrsta_vozila", array('value' => $vrsta_vozila)) ?>
            <td><?= $this->Form->text("{$vrsta_vozila}.{$i}.{$key}.min", array('value'=>$row['min'], 'class'=>'number')) ?></td>
            <td><?= $this->Form->text("{$vrsta_vozila}.{$i}.{$key}.max", array('value'=>$row['max'], 'class'=>'number')) ?></td>
            <td><?= $this->Form->text("{$vrsta_vozila}.{$i}.{$key}.value", array('value'=>$row['value'], 'class'=>'number')) ?></td>
            <td class="actions">
                <a href="javascript:void(0)" class="add" onclick="addItem(this)">+</a>
                <a href="javascript:void(0)" class="delete" onclick="deleteItem(this)">-</a>
            </td>
        </tr>
        <? endforeach ?>
    </table>
    <? endforeach ?>

<?= $this->Form->submit('Sačuvaj') ?>
<?= $this->Form->end() ?>

<script type="text/javascript">
function osiguranje_cene_complete(req) {
    alert(req.responseText);
}
function addItem(a) {
    var tr = $(a).parent().parent();
    var index = parseInt(tr.attr('index'));
    
    if (index >= 0) {
        c = tr.clone();
        index++;
        c.attr('index', index);
        c.find('input').each(function(){
            var name = $(this).attr('name');
            if (name.length) {
                $(this).attr('name', name.replace(/\[\d+\]/i, "["+index+"]"));
                if (name.match(/\[id\]$/)) {
                    $(this).val('');
                }
            } 
        });
        tr.parent().append(c);
    }
}
function deleteItem(a) {
    if (confirm('Da li želite da obrišete cenu?')) {
        $(a).parent().parent().remove();
    }
}
</script>
